/**********************************************************************
 * FILE : JettyRun.java
 * CREATE DATE : 2011-12-8
 * DESCRIPTION :
 *		
 *      
 * CHANGE HISTORY LOG
 *---------------------------------------------------------------------
 * NO.|    DATE    |     NAME     |     REASON     | DESCRIPTION
 *---------------------------------------------------------------------
 * 1  | 2011-12-8 |  Sting  |    草创版本
 *---------------------------------------------------------------------              
 **********************************************************************
 */
package com.sgm.de.benchmark.lancher;

import java.util.EventListener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.wicket.protocol.http.ContextParamWebApplicationFactory;
import org.apache.wicket.protocol.http.WicketFilter;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.component.LifeCycle;

import com.sgm.de.benchmark.WebAgent;
import com.sgm.de.benchmark.lancher.web.Dashboard;

/**
 * @author Sting
 * TODO 主要作用:
 *		注意事项:
 */
public class JettyLancher {

    // change these accordingly:
    private static final String LISTEN_HOST = "localhost";
    private static final int LISTEN_PORT = 8888;
    private static final String WICKET_WEBAPP_CLASS_NAME = Dashboard.class.getName();

    public static final void main(String[] args) throws Exception {
        Server server = new Server();
        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setHost(LISTEN_HOST);
        connector.setPort(LISTEN_PORT);
        server.setConnectors(new Connector[] { connector });

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        context.addServlet(DefaultServlet.class, "/*");

        FilterHolder filterHolder = new FilterHolder(WicketFilter.class);
        filterHolder.setInitParameter(ContextParamWebApplicationFactory.APP_CLASS_PARAM, WICKET_WEBAPP_CLASS_NAME);
        filterHolder.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, "/*");
        context.addFilter(filterHolder, "/*", FilterMapping.REQUEST);

        context.addEventListener(new ServletContextListener() {
            private WebAgent agent;

            @Override
            public void contextInitialized(ServletContextEvent sce) {
                agent = new WebAgent();
            }

            @Override
            public void contextDestroyed(ServletContextEvent sce) {
                if (agent != null) {
                    agent.stop();
                }
            }
        });

        server.setHandler(context);

        try {
            System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
            server.start();
            System.in.read();
            System.out.println(">>> STOPPING EMBEDDED JETTY SERVER");
            while (System.in.available() == 0) {
                Thread.sleep(5000);
            }
            server.stop();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(100);
        }
    }
}
